﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;

namespace luanvanFinal
{
    /// <summary>
    /// Interaction logic for wdStageDetailShow.xaml
    /// </summary>
    public partial class wdStageDetailShow : Window
    {
        int idProject;
        String title;
        public wdStageDetailShow()
        {
            InitializeComponent();
        }
        public int IdProject
        {
            get { return idProject; }
            set { idProject = value; }
        }
        public String Title
        {
            get { return title; }
            set { title = value; }
        }
        public void loadForm()
        {
            int STT = 1;
            clsProject project = new clsProject();
            tbProject.Text = project.getNameFromId(idProject);
            tbStage.Text = title;
            clsStage stage = new clsStage();
            String SQLStage = "select Id from stage where idProject='" + idProject + "' and title='"+title+"'";
            DataTable dtStage = stage.getDataStage(SQLStage);
            clsStageDetail stageDetail = new clsStageDetail();            
            String SQLStageDetail = "select * from stageDetail where idStage='"+Convert.ToInt32(dtStage.Rows[0][0].ToString())+"'";
            DataTable dt = stageDetail.getDataStageDetail(SQLStageDetail);
            DataTable getDt = new DataTable();
            getDt.Columns.Add("Num");
            getDt.Columns.Add("Stage Detail Title");
            getDt.Columns.Add("Detail");
            getDt.Columns.Add("Work Percent");            
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = getDt.NewRow();
                dr[0] = STT.ToString();
                dr[1] = dt.Rows[i][1].ToString();
                dr[2] = dt.Rows[i][3].ToString();
                dr[3] = dt.Rows[i][4].ToString();                
                STT++;
                getDt.Rows.Add(dr);
                getDt.AcceptChanges();
            }
            dgStageDetail.ItemsSource = getDt.DefaultView;
        }

        public void delete()
        {
            try
            {
                DataRowView row = (DataRowView)dgStageDetail.SelectedItems[0];
                String StageDetailTitle = row[1].ToString();
                String SQL = "select Id from stage where idProject = '" + idProject + "' and title = '" + title + "'";
                clsStage stage = new clsStage();
                int idStage = Convert.ToInt32(stage.getDataStage(SQL).Rows[0][0].ToString());
                clsStageDetail stageDetail = new clsStageDetail();
                stageDetail.IdStage = idStage;
                stageDetail.Title = StageDetailTitle;
                stageDetail.deleteStageDetail();
                loadForm();
            }
            catch
            {

            }
        }
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            DataRowView row = (DataRowView)dgStageDetail.SelectedItems[0];
            String stageDetailTitle = row[1].ToString();
            String SQL = "select Id from stage where idProject='" + idProject + "' and title='" + title + "'";
            clsStage stage = new clsStage();
            DataTable dt = stage.getDataStage(SQL);
            int idStage = Convert.ToInt32(dt.Rows[0][0].ToString());
            wdStageDetailEdit stageDetail = new wdStageDetailEdit();
            stageDetail.IdStage = idStage;
            stageDetail.Title = stageDetailTitle;
            stageDetail.Show();
        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            DataRowView row = (DataRowView)dgStageDetail.SelectedItems[0];
            String stageDetailTitle = row[1].ToString();
            String SQL = "select Id from stage where idProject='" + idProject + "' and title='" + title + "'";
            clsStage stage = new clsStage();
            DataTable dt = stage.getDataStage(SQL);
            int idStage = Convert.ToInt32(dt.Rows[0][0].ToString());
            wdStageDetailUpdate stageDetail = new wdStageDetailUpdate();
            stageDetail.IdStage = idStage;
            stageDetail.Title = stageDetailTitle;
            stageDetail.Show();
        }

        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            delete();
        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            loadForm();
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            DataRowView row = (DataRowView)dgStageDetail.SelectedItems[0];
            String stageDetailTitle = row[1].ToString();
            String SQL = "select Id from stage where idProject='"+idProject+"' and title='"+title+"'";
            clsStage stage = new clsStage();
            DataTable dt = stage.getDataStage(SQL);
            int idStage = Convert.ToInt32(dt.Rows[0][0].ToString());
            wdStageDetailEdit stageDetail = new wdStageDetailEdit();
            stageDetail.IdStage = idStage;
            stageDetail.Title = stageDetailTitle;            
            stageDetail.Show();
        }

        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            DataRowView row = (DataRowView)dgStageDetail.SelectedItems[0];
            String stageDetailTitle = row[1].ToString();
            String SQL = "select Id from stage where idProject='" + idProject + "' and title='" + title + "'";
            clsStage stage = new clsStage();
            DataTable dt = stage.getDataStage(SQL);
            int idStage = Convert.ToInt32(dt.Rows[0][0].ToString());
            wdStageDetailUpdate stageDetail = new wdStageDetailUpdate();
            stageDetail.IdStage = idStage;
            stageDetail.Title = stageDetailTitle; 
            stageDetail.Show();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            delete();
        }
    }
}
